﻿using System;
using System.Text;

namespace NDBGen.Plugins.Descriptors.Columns
{
    internal static class ColumnMetadataHelper
    {
        public static readonly String[] NoCustomAttributes = new String[0];
        public static readonly String[] NoColumnAttributeDeclarations = new String[0];
        public static readonly String NoSql = String.Empty;

        public static String CreateColumnAttribute(String columnName, params Object[] properties)
        {
            var builder = new StringBuilder(String.Format("[Column(Name = \"{0}\"", columnName));
            foreach (var property in properties.SortOrdered())
            {
                builder.Append(", ");
                builder.Append(property);
            }

            builder.Append(")]");

            return builder.ToString();
        }

        public static class ColumnAttribute
        {
            public static class DbType
            {
                private const Int32 INDEX = 0;
                private const String KEY = "DbType";

                public static readonly OrderedKeyedMetadataValue Format = new OrderedKeyedMetadataValue(INDEX, KEY, "DbType = \"{0}{1} null\"");
            }

            public static class CanBeNull
            {
                private const Int32 INDEX = 1;
                private const String KEY = "CanBeNull";

                public static readonly OrderedKeyedMetadataValue True = new OrderedKeyedMetadataValue(INDEX, KEY, "CanBeNull = true");
                public static readonly OrderedKeyedMetadataValue False = new OrderedKeyedMetadataValue(INDEX, KEY, "CanBeNull = false");
            }

            public static class AutoSync
            {
                private const Int32 INDEX = 2;
                private const String KEY = "AutoSync";

                public static readonly OrderedKeyedMetadataValue OnInsert = new OrderedKeyedMetadataValue(INDEX, KEY, "AutoSync = AutoSync.OnInsert");
                public static readonly OrderedKeyedMetadataValue Always = new OrderedKeyedMetadataValue(INDEX, KEY, "AutoSync = AutoSync.Always");
                public static readonly OrderedKeyedMetadataValue Never = new OrderedKeyedMetadataValue(INDEX, KEY, "AutoSync = AutoSync.Never");
                public static readonly OrderedKeyedMetadataValue OnUpdate = new OrderedKeyedMetadataValue(INDEX, KEY, "AutoSync = AutoSync.OnUpdate");
            }

            public static class IsDbGenerated
            {
                private const Int32 INDEX = 3;
                private const String KEY = "IsDbGenerated";

                public static readonly OrderedKeyedMetadataValue True = new OrderedKeyedMetadataValue(INDEX, KEY, "IsDbGenerated = true");
                public static readonly OrderedKeyedMetadataValue False = new OrderedKeyedMetadataValue(INDEX, KEY, "IsDbGenerated = false");
            }

            public static class IsPrimaryKey
            {
                private const Int32 INDEX = 4;
                private const String KEY = "IsPrimaryKey";

                public static readonly OrderedKeyedMetadataValue True = new OrderedKeyedMetadataValue(INDEX, KEY, "IsPrimaryKey = true");
                public static readonly OrderedKeyedMetadataValue False = new OrderedKeyedMetadataValue(INDEX, KEY, "IsPrimaryKey = false");
            }

            public static class IsVersion
            {
                private const Int32 INDEX = 5;
                private const String KEY = "IsVersion";

                public static readonly OrderedKeyedMetadataValue True = new OrderedKeyedMetadataValue(INDEX, KEY, "IsVersion = true");
                public static readonly OrderedKeyedMetadataValue False = new OrderedKeyedMetadataValue(INDEX, KEY, "IsVersion = false");
            }

            public static class UpdateCheck
            {
                private const Int32 INDEX = 6;
                private const String KEY = "UpdateCheck";

                public static readonly OrderedKeyedMetadataValue Always = new OrderedKeyedMetadataValue(INDEX, KEY, "UpdateCheck = UpdateCheck.Always");
                public static readonly OrderedKeyedMetadataValue Never = new OrderedKeyedMetadataValue(INDEX, KEY, "UpdateCheck = UpdateCheck.Never");
            }
        }

        public static class CustomAttributes
        {
            public static readonly OrderedKeyedMetadataValue Required = new OrderedKeyedMetadataValue(0, "Required", "[Required]");
            public static readonly OrderedKeyedMetadataValue StringLengthFormat = new OrderedKeyedMetadataValue(0, "StringLength", "[StringLength({0})]");
            public static readonly OrderedKeyedMetadataValue DataMemberFormat = new OrderedKeyedMetadataValue(1, "DataMember", "[DataMember(Name = \"{0}\")]");
            public static readonly OrderedKeyedMetadataValue ForeignKeyForFormat = new OrderedKeyedMetadataValue(2, "ForeignKeyFor", "[ForeignKeyFor(typeof({0}), \"{1}\")]");
            public static readonly OrderedKeyedMetadataValue LookupKeyMember = new OrderedKeyedMetadataValue(3, "LookupKeyMember", "[LookupKeyMember]");
        }

        internal static readonly Char[] ReservedTokenChars = new Char[] { '.' };
    }
}